package com.ease.actionscript.mapping.util;

import java.sql.Clob;
import java.util.HashMap;
import java.util.Map;

import com.ease.actionscript.mapping.type.ActionScriptValueAccessor;
import com.ease.actionscript.mapping.vo.ActionScriptTypeDefinition;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class ActionScriptTypeConverter {

	public static final String NON_PRIMITIVE_ARRAY_PREFIX = "[L";

	private final static Map<String, ActionScriptTypeDefinition> types = new HashMap<String, ActionScriptTypeDefinition>();

	private static String canonicalName(Class<?> javaType) {
		return javaType.getCanonicalName();
	}

	public static ActionScriptTypeDefinition get(Class<?> javaType) {
		ActionScriptTypeDefinition definition = types.get(canonicalName(javaType));
		return definition;
	}

	public static void put(Class<?> javaType, String actionScriptType, String actionScriptDefaultValue, ActionScriptValueAccessor actionScriptValueHandler) {
		ActionScriptTypeDefinition definition = new ActionScriptTypeDefinition();
		definition.setJavaType(javaType);
		definition.setActionScriptType(actionScriptType);
		definition.setActionScriptDefaultValue(actionScriptDefaultValue);
		definition.setActionScriptValueHandler(actionScriptValueHandler);
		types.put(canonicalName(javaType), definition);
	}

	public static void put(Class<?> javaType, String actionScriptType, String actionScriptDefaultValue) {
		put(javaType, actionScriptType, actionScriptDefaultValue, null);
	}

	public static void put(Class<?> javaType, String actionScriptType, ActionScriptValueAccessor actionScriptValueHandler) {
		put(javaType, actionScriptType, "null", actionScriptValueHandler);
	}

	public static void put(Class<?> javaType, String actionScriptType) {
		put(javaType, actionScriptType, "null", null);
	}

	static {
		//
		put(boolean.class, "Boolean", "false");
		put(Boolean.class, "Boolean", "false");
		//
		put(byte.class, "int", "0");
		put(Byte.class, "int", "0");
		put(short.class, "int", "0");
		put(Short.class, "int", "0");

		put(int.class, "int", "0");
		put(Integer.class, "int", "0");
		//
		put(long.class, "Number", "0");
		put(Long.class, "Number", "0");
		put(float.class, "Number", "0");
		put(Float.class, "Number", "0");
		put(double.class, "Number", "0");
		put(Double.class, "Number", "0");
		//
		put(java.math.BigDecimal.class, "Number", "0");
		//
		put(char.class, "String");
		put(String.class, "String");
		put(Clob.class, "String");
		//
		put(java.sql.Blob.class, "flash.utils.ByteArray");
		//
		put(java.sql.Array.class, "Array");
		put(java.lang.reflect.Array.class, "Array");
		//
		put(java.util.Collection.class, "mx.collections.ArrayCollection");
		put(java.util.List.class, "mx.collections.ArrayCollection");/*
																	 * [ArrayElementType (
																	 * "com.posadas.linc.entidades.AdminHotel" )]
																	 */
		put(java.util.ArrayList.class, "mx.collections.ArrayCollection");
		put(java.util.LinkedList.class, "mx.collections.ArrayCollection");
		//
		put(java.lang.Class.class, "Class");
		//
		put(java.lang.Object.class, "Object");
		put(java.util.Set.class, "Object");
		put(java.util.HashSet.class, "Object");
		put(java.util.Map.class, "Object");
		put(java.util.HashMap.class, "Object");
		put(java.util.LinkedHashMap.class, "Object");
		//
		put(java.io.InputStream.class, "flash.utils.ByteArray");
		//
		put(java.sql.Date.class, "Date");
		put(java.sql.Time.class, "Date");
		put(java.util.Date.class, "Date");
		put(java.sql.Timestamp.class, "Date");
		//
		put(char[].class, "String");
		put(byte[].class, "flash.utils.ByteArray");
		put(short[].class, "Array");
		put(int[].class, "Array");
		put(long[].class, "Array");
		put(double[].class, "Array");
		//
		put(byte[][].class, "flash.utils.ByteArray");
		put(short[][].class, "Array");
		put(int[][].class, "Array");
		put(long[][].class, "Array");
		put(double[].class, "Array");
	}
}
